package com.gzbugu.bugusys.service;

import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;

import com.gzbugu.bugusys.bean.PageModel;
import com.gzbugu.bugusys.dao.IBaseDAO;
import com.gzbugu.bugusys.util.IHQLQuery;
import com.gzbugu.bugusys.vo.Reimburse;
import com.gzbugu.bugusys.vo.Staff;

/**
 * 查询、统计报销信息service
 * 
 * @author gauze
 * 
 */
@Transactional
public class FindReimburseService extends HibernateDaoSupport {
	@Resource(name = "baseDAO")
	private IBaseDAO baseDAO;
	@Resource(name = "hQLQuery")
	IHQLQuery hQLQuery;

	/**
	 * 按个人搜索
	 * 
	 * @param staffId
	 *            员工编号
	 * @return 返回查询结果
	 */
	public Set<Reimburse> findReimburse(String staffId) {
		Staff staff = (Staff) baseDAO.findById(staffId,
				com.gzbugu.bugusys.fields.VoClasseFields.STAFF);
		Set<Reimburse> set = new HashSet<Reimburse>();
		if (staff != null)
			set = staff.getReimburses();
		return set;
	}

	/**
	 * 指定时间区间汇总
	 * 
	 * @param start
	 *            开始时间
	 * @param end
	 *            结束时间
	 * @param page
	 *            PageModel instance
	 * @return 查询结果
	 */
	public List findReimburse(Date start, Date end, PageModel page) {
		String hql = "from Reimburse reimburse where reimburse.start >= ? and reimburse.start <= ? order by reimburse.start desc";
		Query query = this.getSession().createQuery(hql);
		query.setDate(0, start);
		query.setDate(1, end);
		query.setFirstResult((page.getCurrentPage() - 1) * page.getPageSize());
		query.setMaxResults(page.getPageSize());
		return query.list();
	}

	/**
	 * 按月汇总
	 * 
	 * @param year
	 *            年
	 * @param month
	 *            月
	 * @param page
	 *            PageModel instance
	 * @return 查询结果
	 */
	public List fineReimburse(int year, int month, PageModel page) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month - 1, 1);
		Date start = calendar.getTime();// 开始日期为指定月份第一天
		calendar.set(Calendar.DATE, calendar.getMaximum(Calendar.DATE));
		Date end = calendar.getTime();// 结束日期为指定月份的最后一天
		return this.findReimburse(start, end, page);
	}

	/**
	 * 按个人搜索
	 * 
	 * @param staffId
	 *            员工编号
	 * @param page
	 *            PageModel instance
	 * @return 查询结果
	 */
	public List findReimburse(String staffId, PageModel page) {
		String hql = "from Reimburse reimburse where reimburse.staff.staffId = "
				+ staffId + " order by reimburse.start asc";
		return hQLQuery.find(hql, page);
	}

	/**
	 * 查找全部
	 * 
	 * @param page
	 *            PageModel instance
	 * @return 查询结果
	 */
	public List findReimburse(PageModel page) {
		String hql = "from Reimburse";
		return hQLQuery.find(hql, page);
	}

	/**
	 * 统计全部报销信息数据量
	 * 
	 * @return 统计结果
	 */
	public Long countReimburse() {
		String hql = "select count(*) from Reimburse";
		List list = hQLQuery.find(hql);
		if (list != null)
			return (Long) list.get(0);
		return 0L;
	}

	/**
	 * 统计报销信息数据量
	 * 
	 * @param staffId
	 *            员工编号
	 * @return 统计结果
	 */
	public Long countReimburse(String staffId) {
		String hql = "select count(*) from Reimburse reimburse where reimburse.staff.staffId = "
				+ staffId;
		List list = hQLQuery.find(hql);
		if (list != null)
			return (Long) list.get(0);
		return 0L;
	}

	/**
	 * 统计报销信息数据量
	 * 
	 * @param start
	 *            开始日期
	 * @param end
	 *            结束日期
	 * @return 统计结果
	 */
	public Long countReimburse(Date start, Date end) {
		String hql = "select count(*) from Reimburse reimburse where reimburse.start >= ? and reimburse.start<= ?";
		Query query = this.getSession().createQuery(hql);
		query.setDate(0, start);
		query.setDate(1, end);
		List list = query.list();
		return (Long) list.get(0);
	}

	/**
	 * 统计报销信息数据量
	 * 
	 * @param year
	 *            年份
	 * @param month
	 *            月份
	 * @return 统计结果
	 */
	public Long countReimburse(int year, int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month - 1, 1, 0, 0, 0);
		Date start = calendar.getTime();// 开始日期为指定月份第一天
		calendar.set(Calendar.DATE, calendar.getMaximum(Calendar.DATE));
		Date end = calendar.getTime();// 结束日期为指定月份的最后一天
		return this.countReimburse(start, end);
	}

}
